/**
 * 
 */
package com.ffcs.crmd.tsp.server.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections.CollectionUtils;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.server.job.info.JobDetailShell;
import com.ffcs.crmd.tsp.server.job.info.JobDetailShellChannelPairInfo;
import com.ffcs.crmd.tsp.server.job.info.JobDetailTrack;
import com.ffcs.crmd.tsp.server.job.service.JobProcessQueue;
import com.ffcs.crmd.tsp.server.util.PublicMethodHelper;

import io.netty.channel.Channel;

/**
 * 功能说明:任务处理管理者
 *
 * @author ZHONGFUHUA
 * 
 * @since 2.0.0-SNAPSHOT
 *
 */
public class JobProcessManager {
    
    /**
     * 日志
     */
    private final static Logger                   LOG             = LoggerFactory.getLogger(LoggerName.ServerLoggerName);
    
    /**
     * 任务执行表
     */
    private volatile Map<String, JobProcessQueue> jobProcessTable = new ConcurrentHashMap<String, JobProcessQueue>();
    
    /**
     * 任务通道表
     */
    private volatile Map<String, Vector<Channel>> channelJobTable = new ConcurrentHashMap<String, Vector<Channel>>();
    
    /**
     * 
     * 功能说明:添加任务通道信息
     * 
     * @param jobName 任务名
     * @param channel 通道
     */
    public void putChannelJob(String jobName, Channel channel) {
        Vector<Channel> jobChannelList = this.channelJobTable.get(jobName);
        if (jobChannelList == null) {
            jobChannelList = new Vector <Channel>();
        }
        if(!jobChannelList.contains(channel)){
            jobChannelList.add(channel);
        }
        this.channelJobTable.put(jobName, jobChannelList);
    }
    
    /**
     * 
     * 功能说明:添加任务通道信息
     * 
     * @param jobName 任务名
     * @param channel 通道
     */
    public void removeChannelJob(String jobName, Channel channel) {
        Vector<Channel> jobChannelList = this.channelJobTable.get(jobName);
        if(CollectionUtils.isNotEmpty(jobChannelList) && jobChannelList.contains(channel)){
            jobChannelList.remove(channel);
        }
        this.channelJobTable.put(jobName, jobChannelList);
    }
    
    /**
     * 
     * 功能说明: 把对应任务的子任务放入缓存中
     * 
     * @param jobName 任务名
     * @param jobDetailShell 任务运行信息组合
     */
    public void putJobDetail(String jobName, JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        if (jobProcessQueue == null) {
            jobProcessQueue = put(jobName);
        }
        jobProcessQueue.putJobDetail(jobDetailShell);
    }
    
    /**
     * 
     * 功能说明:添加需要重试的子任务
     * 
     * @param jobName 任务名
     * @param jobDetailShell 任务运行信息组合
     */
    public void putJobDetailRetry(JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        jobProcessQueue.putJobDetailRetry(jobDetailShell);
        LOG.warn("SERVER-SCHEDULER: put job detail retry[{}] id=[{}] to queue", jobDetailShell.getJobKey(), jobDetailShell.getId());
    }
    
    /**
     * 
     * 功能说明:已发送执行端处理的子任务放置临时缓存
     * 
     * @param jobName 任务名
     * @param channel 通道
     * @param jobDetailShell 子任务运行内核
     */
    public void putJobDetailRunning(JobDetailShell jobDetailShell, Channel channel) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        jobProcessQueue.putJobDetailRunning(channel, jobDetailShell);
    }
    
    
    /**
     * 
     * 功能说明:添加死任务
     * 
     * @param jobName 任务名
     * @param jobDetailShell 子任务运行内核
     */
    public void putJobDetailDeath(JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        jobProcessQueue.putJobDetailDeath(jobDetailShell);
        LOG.error("SERVER-SCHEDULER: put job detail death[{}] id=[{}]", jobDetailShell.getJobKey(), jobDetailShell.getId());
    }
    
    /**
     * 
     * 功能说明:取出指定任务下的子任务
     * 
     * @param jobName 任务名称
     * @param num 数量
     * @return 待执行任务
     */
    public List<JobDetailShell> takeJobDetail(String jobName, int num) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        List<JobDetailShell> jobDetailShell = jobProcessQueue.takeJobDetail(num);
        return jobDetailShell;
    }
    
    /**
     * 
     * 功能说明:查询获取指定任务下的死子任务
     * 
     * @param jobName 任务名称
     * @param jobDetailName 数量
     * @return 待执行任务
     */
    public List<JobDetailShell> getJobDetailDeath(String jobName, String jobDetailName) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        if (jobProcessQueue != null) {
            return jobProcessQueue.getJobDetailDeath(jobDetailName);
        } else {
            LOG.warn("SERVER-SCHEDULER: get job detail death[{}.{}] fail because jobProcessQueue isn't exist", jobName, jobDetailName);
        }
        return new ArrayList<JobDetailShell>();
    }
    
    /**
     * 
     * 功能说明:查询获取指定死任务的运行轨迹
     * 
     * @param jobName 任务名称
     * @param jobDetailName 数量
     * @return 待执行任务
     */
    public JobDetailTrack getJobDetailTrack(String jobName, String id) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        if (jobProcessQueue != null) {
            return jobProcessQueue.getJobDetailTrack(id);
        } else {
            LOG.warn("SERVER-SCHEDULER: get job detail track[{}] id=[{}] fail because jobProcessQueue isn't exist", jobName, id);
        }
        return null;
    }
    
    /**
     * 
     * 功能说明:扫描超时的子任务
     * @return 
     *
     */
    public List<JobDetailShell> getJobDetailTimeout(String jobName) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        if (jobProcessQueue != null) {
            return jobProcessQueue.getJobDetailTimeout();
        } else {
            LOG.warn("SERVER-SCHEDULER: get timeout job[{}] fail because jobProcessQueue isn't exist", jobName);
        }
        return new ArrayList<JobDetailShell>();
    }
    
    /**
     * 
     * 功能说明:扫描离线的子任务
     * @return 
     *
     */
    /*public List<JobDetailShell> getJobDetailOffline(String jobName) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        if (jobProcessQueue != null) {
            return jobProcessQueue.getJobDetailOffline();
        } else {
            LOG.warn("SERVER-SCHEDULER: get offline job[{}] fail because jobProcessQueue isn't exist", jobName);
        }
        return new ArrayList<JobDetailShell>();
    }*/
    
    
    
    /**
     * 
     * 功能说明:清空重试子任务到待运行表中
     *
     */
    public void flushJobDetailRetry() {
        Iterator<JobProcessQueue> jobProcessQueueIt = jobProcessTable.values().iterator();
        while (jobProcessQueueIt.hasNext()) {
            jobProcessQueueIt.next().flushJobDetailRetry();
        }
    }
    
    /**
     * 
     * 功能说明:更新任务超时时间
     * 
     * @param jobName 任务名称
     * @param timeout 超时时间
     */
    public void updateJobExecuteTimeout(String jobName, long timeout) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        if (jobProcessQueue != null) {
            jobProcessQueue.updateTimeout(timeout);
        } else {
            LOG.warn("SERVER-SCHEDULER: update job[{}] execute timeout=[{}] fail because jobProcessQueue isn't exist", jobName,
                timeout == -1 ? "不超时"
                    : timeout);
        }
    }
    
    /**
     * 
     * 功能说明:更新任务超时时间
     * 
     * @param jobName 任务名称
     * @param timeout 超时时间
     */
    public void updateJobTimeoutPolicy(String jobName, Short timeoutPolicy) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        if (jobProcessQueue != null) {
            if (timeoutPolicy == null) {
                jobProcessQueue.updateTimeoutPolicy((short) 1);
            } else {
                jobProcessQueue.updateTimeoutPolicy(timeoutPolicy.shortValue());
            }
        } else {
            LOG.warn("SERVER-SCHEDULER: update job[{}] timeout policy[{}] fail because jobProcessQueue isn't exist", jobName,
                PublicMethodHelper.getTimeoutPolicy(timeoutPolicy));
        }
    }
    
    /**
     * 
     * 功能说明:更新死任务重置延迟时间
     * 
     * @param jobName 任务名称
     * @param timeout 死任务重置延迟时间
     */
    public void updateResetDeathDelayTime(String jobName, long resetDeathDelayTime) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        if (jobProcessQueue != null) {
            jobProcessQueue.updateResetDeathDelayTime(resetDeathDelayTime);
        } else {
            LOG.warn("SERVER-SCHEDULER: update job[{}] reset death delay time[{}] fail because jobProcessQueue isn't exist", jobName,
                resetDeathDelayTime == -1 ? "不延迟"
                    : resetDeathDelayTime);
        }
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param channel
     * @param jobDetailShell
     */
    public void updateCompleteStatus(Channel channel, JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        jobProcessQueue.updateCompleteStatus(channel, jobDetailShell);
    }
    
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param channel
     * @param jobDetailShell
     */
    public void updateReciveStatus(Channel channel, JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        jobProcessQueue.updateReciveStatus(channel, jobDetailShell);
    }
    
    
    /**
     * 
     * 功能说明:更新任务数量缓冲区大小
     * 
     * @param jobName 任务名称
     * @param bufferLimitSize 缓冲区大小
     */
    public void updateBufferLimitSize(String jobName, int bufferLimitSize) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        if (jobProcessQueue != null) {
            if (bufferLimitSize > 0) {
                jobProcessQueue.updateBufferLimitSize(bufferLimitSize);
            }
        } else {
            LOG.warn("SERVER-SCHEDULER: update job[{}] buffer size limit[{}] fail because jobProcessQueue isn't exist", jobName,
                bufferLimitSize);
        }
    }
    
    /**
     * 
     * 功能说明:清空死任务
     * 
     * @param jobName 任务名
     * @param jobDetailName 子任务名
     * @return 
     */
    public boolean removeJobDetailDeath(JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        if (jobProcessQueue != null) {
            return jobProcessQueue.removeJobDetailDeath(jobDetailShell);
        } else {
            LOG.warn("SERVER-SCHEDULER: remove job detail death[{}] id=[]{} fail because jobProcessQueue isn't exist",
                jobDetailShell.getJobKey(), jobDetailShell.getId());
        }
        return false;
    }
    
    /**
     * 
     * 功能说明:清空运行的子任务
     * 
     * @param jobName 任务名
     * @param jobDetailName 子任务名
     * @return 
     */
    public void removeJobDetailRunning(Channel channel, JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        if (jobProcessQueue != null) {
            jobProcessQueue.removeJobDetailRunning(channel, jobDetailShell);
        } else {
            LOG.warn("SERVER-SCHEDULER: remove job detail running[{}] id=[{}] fail because jobProcessQueue isn't exist",
                jobDetailShell.getJobKey(), jobDetailShell.getId());
        }
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param jobName
     * @param jobDetailName
     */
    public void deleteJobDetail(String jobName, String jobDetailName) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        if (jobProcessQueue != null) {
            jobProcessQueue.deleteJobDetail(jobDetailName);
        } else {
            LOG.warn("SERVER-SCHEDULER: delete job detail[{}] fail because jobProcessQueue isn't exist", jobName, jobDetailName);
        }
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param jobName
     */
    public void deleteJob(String jobName) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.remove(jobName);
        if (jobProcessQueue != null) {
            jobProcessQueue.deleteJob();
        } else {
            LOG.warn("SERVER-SCHEDULER: delete job[{}] fail because jobProcessQueue isn't exist", jobName);
        }
    }
    
    /**
     * 
     * 功能说明:删除执行中的子任务
     * 
     * @param jobName 任务名
     * @param channel 通道
     * @param jobDetailShell 子任务运行内核
     * @return 
     */
    public void completeJobDetailDeath(JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        jobProcessQueue.completeJobDetailDeath(jobDetailShell);
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param jobName
     * @param channel
     * @param jobDetailShell
     */
    /*public void completeJobDetailOld(Channel channel, JobDetailShell jobDetailShell) {
        completeJobDetailOld(channel, jobDetailShell, null);
    }*/
    
    /**
     * 
     * 功能说明:删除执行中的子任务
     * 
     * @param jobName 任务名
     * @param channel 通道
     * @param jobDetailShell 子任务运行内核
     * @return 
     */
    /*public void completeJobDetailOld(Channel channel, JobDetailShell jobDetailShell, JobExecutionException jee) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        jobProcessQueue.completeJobDetailOld(channel, jobDetailShell, jee);
    }*/
    
    /**
     * 
     * 功能说明:
     * 
     * @param jobName
     * @param channel
     * @param jobDetailName
     * @param id
     */
    public void completeJobDetail(String jobName, Channel channel, String jobDetailName, String id) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        jobProcessQueue.completeJobDetail(channel, jobDetailName, id, null);
    }
    
    /**
     * 
     * 功能说明:删除执行中的子任务
     * 
     * @param jobName
     * @param channel
     * @param jobDetailName
     * @param id
     * @param jee
     */
    public void completeJobDetail(String jobName, Channel channel, String jobDetailName, String id, JobExecutionException jee) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        jobProcessQueue.completeJobDetail(channel, jobDetailName, id, jee);
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param jobName 任务名
     * @param jobDetailShell 子任务运行内核
     */
    public void removeJobDetailTrack(JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        jobProcessQueue.removeJobDetailTrack(jobDetailShell);
    }
    
    /**
     * 
     * 功能说明:遍历清理任务通道缓存
     * 
     * @param channel 通道
     */
    public void removeChannelJobAndOffline(Channel channel) {
        Iterator<Entry<String, Vector<Channel>>> channelJobInfoIt = channelJobTable.entrySet().iterator();
        while (channelJobInfoIt.hasNext()) {
            Entry<String, Vector<Channel>> channelJobInfoEntry = channelJobInfoIt.next();
            String jobName = channelJobInfoEntry.getKey();
            Vector<Channel> jobChannelList = channelJobInfoEntry.getValue();
            boolean exist = jobChannelList.remove(channel);
            
            if(exist){
                JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
                if (jobProcessQueue != null) {
                    jobProcessQueue.offlineJobDetailRunning(channel);
                    LOG.debug("SERVER-SCHEDULER: update offline job detail running[{}] channel[{}]", jobName, channel);
                } else {
                    LOG.warn("SERVER-SCHEDULER: update offline job detail running[{}] fail because jobProcessQueue isn't exist", jobName);
                }
            }
        }
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param channel
     * @return
     */
    public List<JobDetailShell> getJobDetailOffline(Channel channel){
        List<JobDetailShell> jobDetailShellList = new ArrayList<JobDetailShell>();
        for(JobProcessQueue jobProcessQueue : jobProcessTable.values()){
            jobDetailShellList.addAll(jobProcessQueue.getJobDetailOffline(channel));
        }
        return jobDetailShellList;
    }
    
    
    /**
     * 
     * 功能说明:遍历清理任务通道缓存
     * 
     * @param channel 通道
     */
    public void removeChannelJobAndShutdown(Channel channel) {
        Iterator<Entry<String, Vector<Channel>>> channelJobInfoIt = channelJobTable.entrySet().iterator();
        while (channelJobInfoIt.hasNext()) {
            Entry<String, Vector<Channel>> channelJobInfoEntry = channelJobInfoIt.next();
            String jobName = channelJobInfoEntry.getKey();
            Vector<Channel> jobChannelList = channelJobInfoEntry.getValue();
            boolean exist = jobChannelList.remove(channel);
            
            if(exist){
                JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
                if (jobProcessQueue != null) {
                    jobProcessQueue.shutdownJobDetailRunning(channel);
                } else {
                    LOG.warn("SERVER-SCHEDULER: update shutdown job detail running[{}] because jobProcessQueue isn't exist", jobName);
                }
            }
        }
    }
    
    /**
     * 
     * 功能说明:增长运行次数
     * 
     * @param channel 通道
     * @return 
     */
    public long getRuntimes(String jobName, String jobDetailName) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        if (jobProcessQueue == null) {
            LOG.warn("SERVER-SCHEDULER: get job detail runtimes[{}] fail because jobProcessQueue isn't exist", jobName);
        } else {
            return jobProcessQueue.getJobDetailRuntimes(jobDetailName);
        }
        return 0;
    }
    
    /**
     * 
     * 功能说明:获取任务名
     * 
     * @return 任务名
     */
    public List<String> getJobNames() {
        List<String> jobNameList = new ArrayList<String>();
        jobNameList.addAll(jobProcessTable.keySet());
        return jobNameList;
    }
    
    /**
     * 
     * 功能说明:获取子任务名称
     * 
     * @param jobName 任务名
     * @return 子任务名称
     */
    public List<String> getJobDetailNameDeaths(String jobName) {
        List<String> jobDetailNameList = new ArrayList<String>();
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        if (jobProcessQueue == null) {
            LOG.warn("SERVER-SCHEDULER: get job detail names[{}] fail because jobProcessQueue isn't exist", jobName);
        } else {
            jobDetailNameList.addAll(jobProcessQueue.getJobDetailDeathNames());
        }
        return jobDetailNameList;
    }
    
    /**
     * 
     * 功能说明:获取可发送的通道对应的已有正在执行的任务数量Map
     * 
     * @param jobName 任务名
     * @param channelList 任务关联已经注册的通道
     * @return
     */
    public Map<Channel, Integer> getChannelsRunningNum(String jobName) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        List<Channel> channels = getChannels(jobName);
        return jobProcessQueue.getChannelsRunningNum(channels);
    }
    
    /**
     * 
     * 功能说明:获取正在运行的任务
     * 
     * @param jobName 任务名
     * @return 获取正在运行的任务
     */
    public Map<Channel, LinkedList<JobDetailShell>> getJobDetailRunningChannel(String jobName) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        if (jobProcessQueue == null) {
            LOG.warn("SERVER-SCHEDULER: get job detail running[{}] fail because jobProcessQueue isn't exist", jobName);
        } else {
            return jobProcessQueue.getJobDetailRunningChannel();
        }
        return null;
    }
    
    /**
     * 
     * 功能说明:获取正在运行的任务
     * 
     * @param jobName 任务名
     * @return 获取正在运行的任务
     */
    public JobDetailShellChannelPairInfo getJobDetailRunningPairInfo(String jobName, String id) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        if (jobProcessQueue == null) {
            LOG.warn("SERVER-SCHEDULER: get job detail running[{}] id[{}] fail because jobProcessQueue isn't exist", jobName, id);
        } else {
            return jobProcessQueue.getJobDetailRunningPairInfo(id);
        }
        return null;
    }
    
    
    /**
     * 
     * 功能说明:获取正在运行的任务
     * 
     * @param jobName 任务名
     * @return 获取正在运行的任务
     */
    public List<JobDetailShell> getJobDetailRunning(String jobName) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        if (jobProcessQueue == null) {
            LOG.warn("SERVER-SCHEDULER: get job detail running[{}] id[{}] fail because jobProcessQueue isn't exist", jobName);
        } else {
            return jobProcessQueue.getJobDetailRunning();
        }
        return null;
    }
    
    /**
     * 
     * 功能说明:获取待执行的任务
     * 
     * @param jobName 任务名
     * @return 获取正在运行的任务
     */
    public List<JobDetailShell> getJobDetailWaiting(String jobName) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        if (jobProcessQueue == null) {
            LOG.warn("SERVER-SCHEDULER: get job detail waiting[{}] fail because jobProcessQueue isn't exist", jobName);
        } else {
            return jobProcessQueue.getJobDetailWaitting();
        }
        return null;
    }
    
    /**
     * 
     * 功能说明:获取重试任务
     * 
     * @param jobName 任务名
     * @return 获取正在运行的任务
     */
    public List<JobDetailShell> getJobDetailRetry(String jobName) {
        JobProcessQueue jobProcessQueue = jobProcessTable.get(jobName);
        if (jobProcessQueue == null) {
            LOG.warn("SERVER-SCHEDULER: get job detail retry[{}] fail because jobProcessQueue isn't exist", jobName);
        } else {
            return jobProcessQueue.getJobDetailRetry();
        }
        return null;
    }
    
    /**
     * 
     * 功能说明:获取已经运行过该子任务的通道
     * 
     * @param jobName 任务名
     * @param jobDetailShell 子任务内核
     * @return 已经运行过该子任务的通道
     */
    public List<Channel> getChannelsRunBefore(JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        List<Channel> alreadyChannelList = jobProcessQueue.getChannelsRunBefore(jobDetailShell);
        return alreadyChannelList;
    }
    
    /**
     * 
     * 功能说明:获取超时策略
     * 
     * @param jobName 任务名
     * @return 超时策略
     */
    public short getTimeoutPolicy(String jobName) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        return jobProcessQueue.getTimeoutPolicy();
    }
    
    /**
     * 
     * 功能说明:获取死任务重置延迟时间
     * 
     * @param jobName 任务名
     * @return 死任务重置延迟时间
     */
    public long getResetDeathDelayTime(String jobName) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobName);
        return jobProcessQueue.getResetDeathDelayTime();
    }
    
    /**
     * 
     * 功能说明:获取任务通道表
     * 
     * @return 任务通道表
     */
    public List<Channel> getChannels(String jobName) {
        List<Channel> rchannelList = new ArrayList<Channel>();
        Vector<Channel> channelList = channelJobTable.get(jobName);
        if (CollectionUtils.isNotEmpty(channelList)) {
            rchannelList.addAll(channelList);
        }
        return rchannelList;
    }
    
    /**
     * 
     * 功能说明: 最后一次运行的执行端
     * 
     * @param jobName 任务名
     * @param jobDetailShell 子任务运行内核
     * @return 执行端通道
     */
    public Channel getJobDetailRunningLastChannel(JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        return jobProcessQueue.getJobDetailRunningLastChannel(jobDetailShell);
    }
    
    /**
     * 
     * 功能说明: 最后一次运行的执行端
     * 
     * @param jobName 任务名
     * @param jobDetailShell 子任务运行内核
     * @return 执行端通道
     */
    public Channel getJobDetailRunningLastSecondChannel(JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        return jobProcessQueue.getJobDetailRunningLastSecondChannel(jobDetailShell);
    }
    
    /**
     * 
     * 功能说明: 最后一次运行的时间
     * 
     * @param jobName 任务名
     * @param jobDetailShell 子任务运行内核
     * @return 最后发送执行时间
     */
    public Date getJobDetailTrackLastDate(JobDetailShell jobDetailShell) {
        JobProcessQueue jobProcessQueue = this.jobProcessTable.get(jobDetailShell.getJobName());
        return jobProcessQueue.getJobDetailDeathLastDate(jobDetailShell);
    }

    /**
     * 功能说明:
     * 
     * @param name
     */
    public void delete(String name) {
        this.jobProcessTable.remove(name);
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param name
     * @return
     */
    public boolean exist(String name){
        if (this.jobProcessTable.get(name) == null) {
            return false;
        }
        return true;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param name
     * @return 
     */
    public JobProcessQueue put(String name) {
        synchronized (this) {
            if (this.jobProcessTable.get(name) == null) {
                JobProcessQueue jobProcessQueue = new JobProcessQueue(name);
                this.jobProcessTable.put(name, jobProcessQueue);
            } 
        }
        return this.jobProcessTable.get(name);
    }
    
}
